﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Adapter.SISCONVE.Components;
using Adapter.SISCONVE.Helper;
using AdapterSISCONVE.Helper;
using AdapterSISCONVE.Pages;
using Business.ValueObject.SISCONVE;

namespace AdapterSISCONVE.Components
{
    public class Reports : Suite
    {
        private ReportsPage _reportsPage;

        public Reports()
            : base()
        {
            _reportsPage = new ReportsPage(Manager);
        }

        public List<VOPerformanceReportVehicle> GetPerformanceReportVehicles()
        {
            Utilities.DeleteFileDownload(Constant.Reports.FileName.PerformanceReport);

            List<VOPerformanceReportVehicle> result = new List<VOPerformanceReportVehicle>();

            Thread.Sleep(2500);
            _reportsPage.ClickButtonReports();
            _reportsPage.ClickButtonPerformance();
            _reportsPage.ClickButtonAgregarTodosVehiculos();
            Thread.Sleep(2500);
            _reportsPage.ClickButtonGenerarReporte();
            Thread.Sleep(2500);
            _reportsPage.ClickButtonExcel();
            Thread.Sleep(2500);
            _reportsPage.ClickLinkDescarga();
            Thread.Sleep(2500);
            
            DataTable table = ReadExcelFile(Constant.Reports.FileName.PerformanceReport);

            foreach (DataRow row in table.Rows)
            {
                VOPerformanceReportVehicle performanceReportRow = new VOPerformanceReportVehicle
                {
                    MIId = row["Movil"].ToString().Split('.')[0],
                    Fuel = row["Combustible"].ToString(),
                    Dispatches = int.Parse(row["Despachos"].ToString()),
                    Distance = float.Parse(row["Distancia"].ToString()),
                    Volume = float.Parse(row["Volumen"].ToString()),
                    Performance = float.Parse(row["Rendimiento"].ToString()),
                    EngineHours = float.Parse(row["HorasMotor"].ToString()),
                    EnginePerformance = float.Parse(row["RendimientoMotor"].ToString())
                };
                result.Add(performanceReportRow);
            }

            this.CleanUp();

            return result;
        }

        public List<VOReportVehicle> GetReportVehicles()
        {
            Utilities.DeleteFileDownload(Constant.Reports.FileName.ReportVehicles);

            List<VOReportVehicle> result = new List<VOReportVehicle>();
            
            Thread.Sleep(2500);
            _reportsPage.ClickButtonReports();
            _reportsPage.ClickButtonReportAllVehicles();
            Thread.Sleep(2500);
            _reportsPage.ClickButtonFilter();
            Thread.Sleep(2500);
            Thread.Sleep(2500);
            _reportsPage.ClickButtonExcel();
            Thread.Sleep(2500);
            _reportsPage.ClickLinkDescarga();
            Thread.Sleep(2500);
           
            DataTable table = ReadExcelFile(Constant.Reports.FileName.ReportVehicles);

            foreach (DataRow row in table.Rows)
            {
                VOReportVehicle vehicleReport = new VOReportVehicle
                {
                    MIId = row["Movil_Id"].ToString(),
                    Id = row["Matrícula"].ToString(),
                    IdDivision = int.Parse(row["División"].ToString()),
                    NameDivision = row["Nombre_división"].ToString(),
                    OnlyFleet = int.Parse(row["Flota_única"].ToString()),
                    FleetName = row["Nombre_flota"].ToString(),
                    CardId = row["Card_Id"].ToString(),
                    OrganizationId = row["Id_en_organización"].ToString(),
                    UnitId = row["TRU_Unit_Id"].ToString(),
                    TRU_Model = row["TRU_Modelo"].ToString(),
                    Fuel = row["Combustible"].ToString(),
                    Notes = row["Notas"].ToString(),
                    VehicleType = Mapper.MapVehicleTypeToVehicleTypeEnum(row["TipoVehiculo"].ToString()),
                    Brand = row["Marca"].ToString(),
                    Model = row["Modelo"].ToString(),
                    TankCapacity = !string.IsNullOrEmpty(row["CapacidadTanque"].ToString()) ? int.Parse(row["CapacidadTanque"].ToString()) : 0,
                    TankCapacityDesc = row["CapacidadTanqueDesc"].ToString(),
                    Year = !string.IsNullOrEmpty(row["Anio"].ToString()) ? int.Parse(row["Anio"].ToString()) : 0,
                    Active = row["Móvil_activo"].ToString().ToUpper() == "SI" ? true : false,
                    BlackList = row["Móvil_en_lista_negra"].ToString().ToUpper() == "SI" ? true : false,
                    Installed = row["Móvil_instalado"].ToString().ToUpper() == "SI" ? true : false,
                    DispatchKeyRequire = row["Requiere_llave_en_despacho"].ToString().ToUpper() == "SI" ? true : false
                };
                result.Add(vehicleReport);
            }

            this.CleanUp();

            return result;
        }
    }
}